//
//  LDTabBar.swift
//  Weibo201655
//
//  Created by pro on 16/5/5.
//  Copyright © 2016年 LeeDan. All rights reserved.
//

import UIKit

protocol LDTabBarDelegate: NSObjectProtocol {
    func tabBarPlusButtonAction()
}

class LDTabBar: UITabBar {

//    override init(frame: CGRect) {
//        super.init(frame: frame)
//        addSubview(plusButton)//此处添加，加号按钮会被系统的覆盖
//        
//    }
//
//    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }
    
    //代理
    weak var tabBarDelegate: LDTabBarDelegate?
    
    //布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //加号按钮frame
        let width = frame.width / 5
        plusButton.frame = CGRect(x: 0, y: 0, width: width + 5, height: frame.height)
//        plusButton.frame.size = CGSizeMake(width + 5, frame.height)
        plusButton.center = CGPointMake(frame.width * 0.5, frame.height * 0.5)
    }
    
    //MARK: 加号按钮点击
    func plusButtonClick() {
        tabBarDelegate?.tabBarPlusButtonAction()
    }
    
    //MARK: 懒加载
    private lazy var plusButton: UIButton = {
        let btn = UIButton(type: UIButtonType.Custom)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        //尺寸
        btn.sizeToFit()
        //点击事件
        btn.addTarget(self, action: "plusButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        self.addSubview(btn)
        return btn
    }()

}
